//
//  AppUtils.m
//  SHDoctor
//
//  Created by Mac mini on 15/10/21.
//  Copyright © 2015年 ShiHeng. All rights reserved.
//

#import "AppUtils.h"

@implementation AppUtils

//动态 计算行高
//根据字符串的实际内容的多少 在固定的宽度和字体的大小，动态的计算出实际的高度
+ (CGFloat)textHeightFromTextString:(NSString *)text width:(CGFloat)textWidth fontSize:(CGFloat)size{
    
    if (text.length == 0) {
        return 0;
    }
    
//    CGFloat textLength = text.length * (size + 0.5);
//    BOOL moreLine = textLength > textWidth;
    
    UIFont *textFont = [UIFont systemFontOfSize: size];
    CGSize titleSize = [text boundingRectWithSize: CGSizeMake(textWidth, MAXFLOAT) options: NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin attributes: @{NSFontAttributeName: textFont} context:nil].size;
    
    return roundf(titleSize.height);
}
+ (CGFloat)textWidthFromTextString:(NSString *)text height:(CGFloat)textHeight fontSize:(CGFloat)size{
    
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:size]};
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MAXFLOAT, textHeight) options:NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesFontLeading|NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil];
    //返回计算出的行高
    return rect.size.width;
}

//获取iOS版本号
+ (double)getCurrentIOS {
    return [[[UIDevice currentDevice] systemVersion] doubleValue];
}

static NSDateFormatter *dateFormatter;
+ (NSString *)stringFromDate:(NSDate *)date formatter:(NSString *)formatter {
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
    }
    
    [dateFormatter setDateFormat:formatter];
    return [dateFormatter stringFromDate:date];
}

+ (NSString *)stringFromDateTime:(NSTimeInterval)timeInterval formatter:(NSString *)formatter {
    
    NSDate *date = [NSDate dateWithTimeIntervalSinceReferenceDate: timeInterval];
    return [self stringFromDate: date formatter: formatter];
}

+ (NSDate *)dateFromString:(NSString *)dateString formatter:(NSString *)formatter {
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
    }
    [dateFormatter setDateFormat:formatter];
    return [dateFormatter dateFromString:dateString];
}

+ (NSString *)stringFromDateString:(NSString*)dateString fromFormatter:(NSString*)fromFormatter toFormatter:(NSString*)toFormatter {
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
    }
    [dateFormatter setDateFormat: fromFormatter];
    NSDate* date = [dateFormatter dateFromString: dateString];
    [dateFormatter setDateFormat: toFormatter];
    return [dateFormatter stringFromDate: date];
}

+ (NSInteger)ageFromBirthDay:(NSString*)birthDay {
    
    if (birthDay == nil) {
        return 1;
    }
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
    }
    [dateFormatter setDateFormat: @"yyyy-MM-dd"];
    NSDate* date = [dateFormatter dateFromString: birthDay];
    NSTimeInterval timeInterval = [date timeIntervalSinceNow];
    
    NSInteger age = timeInterval / (3600 * 24 * 365);
    
    return labs(age) + 1;
}

+ (NSString*)getProjectVersion {
    
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}


@end
